package org.openkoala.demo.facade.impl;

import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.text.MessageFormat;
import javax.inject.Inject;
import javax.inject.Named;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.Page;
import org.dayatang.querychannel.QueryChannelService;
import org.openkoala.koala.commons.InvokeResult;
import org.openkoala.demo.facade.dto.*;
import org.openkoala.demo.facade.impl.assembler.UserAssembler;
import org.openkoala.demo.facade.UserFacade;
import org.openkoala.demo.application.UserApplication;

import org.openkoala.demo.core.domain.*;

@Named
@Transactional
public class UserFacadeImpl implements UserFacade {

	@Inject
	private UserApplication  application;

	private QueryChannelService queryChannel;

    private QueryChannelService getQueryChannelService(){
       if(queryChannel==null){
          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
       }
     return queryChannel;
    }
	
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public InvokeResult getUser(Long id) {
		return InvokeResult.success(UserAssembler.toDTO(application.getUser(id)));
	}
	
	public InvokeResult saveUser(UserDTO userDTO) {
		application.saveUser(UserAssembler.toEntity(userDTO));
		return InvokeResult.success();
	}
	
	public InvokeResult updateUser(UserDTO userDTO) {
		application.updateUser(UserAssembler.toEntity(userDTO));
		return InvokeResult.success();
	}
	
	public InvokeResult removeUser(Long id) {
		application.removeUser(application.getUser(id));
		return InvokeResult.success();
	}
	
	public InvokeResult removeUsers(Long[] ids) {
		Set<User> users= new HashSet<User>();
		for (Long id : ids) {
			users.add(application.getUser(id));
		}
		application.removeUsers(users);
		return InvokeResult.success();
	}
	
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public List<UserDTO> findAllUser() {
		return UserAssembler.toDTOs(application.findAllUser());
	}
	
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public Page<UserDTO> pageQueryUser(UserDTO queryVo, int currentPage, int pageSize) {
		List<UserDTO> result = new ArrayList<UserDTO>();
		List<Object> conditionVals = new ArrayList<Object>();
	   	StringBuilder jpql = new StringBuilder("select _user from User _user   where 1=1 ");
	   	if (queryVo.getName() != null && !"".equals(queryVo.getName())) {
	   		jpql.append(" and _user.name like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getName()));
	   	}		
	   	if (queryVo.getAge() != null) {
	   		jpql.append(" and _user.age=?");
	   		conditionVals.add(queryVo.getAge());
	   	}	
	   	if (queryVo.getDept() != null && !"".equals(queryVo.getDept())) {
	   		jpql.append(" and _user.dept like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getDept()));
	   	}		
        Page<User> pages = getQueryChannelService()
		   .createJpqlQuery(jpql.toString())
		   .setParameters(conditionVals)
		   .setPage(currentPage, pageSize)
		   .pagedList();
       /** for (User user : pages.getData()) {
           // 将domain转成VO 
                  
         UserDTO userDTO = UserAssembler.EntityToDto(user);       
                                                           result.add(userDTO);
        }**/
        return new Page<UserDTO>(pages.getStart(), pages.getResultCount(),
         pageSize, UserAssembler.toDTOs(pages.getData()));
	}
	
	
}
